clear all
set more off
set maxvar 10000

global path "~/shared_space/dbaharc/BHOR-YugoslavianRefugees/"
global pathtmp "~/shared_space/cidgrowlab/bahartemp/"


/*************/
/*Atlas Data*/
/************/
//keep year export_value location_code partner_code sitc_product_code

forvalues y = 1962/2016 {
    insheet using "~/shared_space/dbaharc/General_Data/Atlas2017/country_partner_sitcproduct4digit_year_`y'.csv", clear
    keep year export_value import_value location_code partner_code sitc_product_code
    destring sitc_product_code, replace force
    tempfile y`y'
    save `y`y''
}

clear
forvalues y = 1962/2016 {
    append using `y`y''
}

ren (location_code partner_code sitc_product_code) (exporter importer sitc)
drop if mi(sitc)

//append using "${path}data/SCGExports.dta" //Exports of SCG that were excluded from the Atlas data

//rename iso1 iso3_d //iso1 is the importer country
//rename iso2 iso3_o //iso2 is the exporter country
ren importer iso3_d
ren exporter iso3_o

drop if iso3_o=="SRB" & year==2005 //Export values are all zero values, anyways
drop if iso3_o=="SCG" & year==2006 //Very small exports for SCG in 2006 as compared to SRB. To be conservative, I delete them, in case they are repeated with some SRB exports

//128 countries of Atlas + YUG, SCG and MNE (SCG is divided in SRB and MNE after 2005)
//ren iso3_d iso3
//merge m:1 iso3 using "${path}data/ISOInSampleAtlas.dta", keep(matched) nogen
//ren iso3 iso3_d
ren iso3_o iso3
merge m:1 iso3 using "${path}data/ISOInSampleAtlas.dta", keep(matched) nogen
ren iso3 iso3_o

//keep if year>=1984 & year<=2014
ren export_value exp
ren import_value imp
ren iso3_d iso3_imp
ren iso3_o iso3_exp
ren sitc product

/*Converting all Former Yugoslavian republic to have the same code, YUG*/
preserve
keep if iso3_exp=="SCG" | iso3_exp=="SRB" | iso3_exp=="KOS" | iso3_exp=="HRV" ///
       | iso3_exp=="SVN" | iso3_exp=="MNE" | iso3_exp=="MKD" | iso3_exp=="BIH" ///
       | iso3_exp=="YUG"
collapse (sum) exp, by(iso3_exp product year)
ren iso3_exp iso3
save "${path}data/ExportsYUGCountriesROW.dta", replace
restore

//Standardizing Serbia and Montenegro with one single iso3
replace iso3_exp = "SRB" if iso3_exp=="SCG"
replace iso3_imp = "SRB" if iso3_imp=="SCG"
replace iso3_exp = "SRB" if iso3_exp=="MNE"
replace iso3_imp = "SRB" if iso3_imp=="MNE"
//There are zero observations that drop here
drop if iso3_exp == "SRB" & iso3_imp =="SRB"


/*Loop used to exclude each one of the countris from total YUG exports*/
foreach c in SRB HRV SVN MKD BIH {
    preserve
    keep if iso3_exp=="`c'"
    drop if iso3_imp == "SRB" | iso3_imp == "KOS" | iso3_imp =="HRV" | iso3_imp == "SVN" | ///
        iso3_imp == "MNE" | iso3_imp == "MKD" | iso3_imp == "BIH"
    drop if iso3_imp =="DEU"
    collapse (sum) exp`c'_xdeu = exp, by(product year)
    g iso3 = "YUG"
    tempfile exports`c'
    save `exports`c''
    restore
}

/*Sum up all countries to become YUG*/
foreach c in SRB KOS HRV SVN MKD BIH {
    replace iso3_exp = "YUG" if iso3_exp=="`c'"
    replace iso3_imp = "YUG" if iso3_imp=="`c'"
}
/*Drop trade within former YUG countries*/
drop if iso3_exp=="YUG" & iso3_imp=="YUG"

collapse (sum) exp imp, by(iso3_exp iso3_imp product year)

sort iso3_exp
egen expid = group(iso3_exp)

sort iso3_imp
egen impid = group(iso3_imp)

sort product
egen productid = group(product)

preserve
keep iso3_exp expid
duplicates drop
save  "${path}data/expID.dta",replace
restore

preserve
keep iso3_imp impid
duplicates drop
save  "${path}data/impID.dta",replace
restore

preserve
keep product productid
duplicates drop
save  "${path}data/ProductsID.dta", replace
restore

replace exp = 0 if mi(exp)
outsheet expid impid productid year exp using "${path}data/bilateralexpraw.out", replace comma nonames

/*Balance the panel in Matlab*/
shell matlab -nodesktop -r "run ${path}m_files/BilateralMatrix.m"

/*Costinot, Donaldson and Konmujer productivity estimation*/
insheet expid impid productid year exp using "${path}data/BalancedBilateral.out", clear
//erase "${path}data/bilateralepraw.out"

merge m:1 expid using "${path}data/expID.dta", keep(master matched) nogen
merge m:1 impid using "${path}data/impID.dta", keep(master matched) nogen
merge m:1 productid using "${path}data/ProductsID.dta", keep(master matched) nogen
replace exp = 0 if mi(exp)

g lnexpplus1 = log(exp+1)
g asinhexp = asinh(exp)
g expmillions = exp/1000000

egen xp = group(iso3_exp product year)
egen mp = group(iso3_imp product year)
egen xm = group(iso3_exp iso3_imp year)

qui reghdfe asinhexp, a(xp mp xm, savefe)
keep iso3_exp product year __hdfe1__
ren __hdfe1__ xpest_asinh
ren iso3_exp iso3
duplicates drop

g xpest = exp(xpest_asinh/6.53)

save "${path}data/CDKasinh.dta", replace




